/**
 *	创建一个线程池
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framerock.server;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 创建一个线程池
 * */
public class ThreadServer {

    private ExecutorService executor;

    private static ThreadServer threadserver = null;


    /**
     * 加入进程里面
     * */
    public static void addThread(Runnable runnable)
    {
        if(threadserver == null)threadserver = new ThreadServer(10);
        threadserver.add(runnable);
    }

    /***
     * 关闭线程池
     */
    public static void closeThread()
    {
        if(threadserver != null){
            threadserver.close();
            threadserver = null;
        }
    }

    public ThreadServer(int nThreads){
        executor = Executors.newFixedThreadPool(nThreads);
        /*
        executor = new ThreadPoolExecutor(10, 10,
                60, TimeUnit.MINUTES,
                new LinkedBlockingQueue<Runnable>()
        );
         */
    }

    /**
     * 加入线程池
     * */
    public void add(Runnable runnable)
    {
        executor.execute(runnable);
    }

    /**
     * 关闭线程池
     * */
    public void close()
    {
        executor.shutdown();
    }
}